package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TourismActivities;
import com.ruoyi.system.domain.TourismUser;
import com.ruoyi.system.mapper.TourismActiviteSignMapper;
import com.ruoyi.system.mapper.TourismActivitiesMapper;
import com.ruoyi.system.mapper.TourismUserMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismActiviteSign;
import com.ruoyi.system.service.ITourismActiviteSignService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 活动报名Controller
 *
 * @author gqd
 * @date 2021-01-16
 */
@RestController
@RequestMapping("/system/sign")
public class TourismActiviteSignController extends BaseController
{
    @Autowired
    private ITourismActiviteSignService tourismActiviteSignService;
    @Autowired
    private TokenService tokenService;
    @Resource
    private TourismUserMapper tourismUserMapper;
    @Resource
    private TourismActivitiesMapper activitiesMapper;
    @Resource
    private TourismActiviteSignMapper tourismActiviteSignMapper;

    /**
     * 查询活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('system:sign:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismActiviteSign tourismActiviteSign)
    {
        startPage();
        List<TourismActiviteSign> list = tourismActiviteSignService.selectTourismActiviteSignList(tourismActiviteSign);
        return getDataTable(list);
    }

    /**
     * 导出活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('system:sign:export')")
    @Log(title = "活动报名", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismActiviteSign tourismActiviteSign)
    {
        List<TourismActiviteSign> list = tourismActiviteSignService.selectTourismActiviteSignList(tourismActiviteSign);
        ExcelUtil<TourismActiviteSign> util = new ExcelUtil<TourismActiviteSign>(TourismActiviteSign.class);
        return util.exportExcel(list, "sign");
    }

    /**
     * 获取活动报名详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sign:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tourismActiviteSignService.selectTourismActiviteSignById(id));
    }

    /**
     * 新增活动报名
     */
    @PreAuthorize("@ss.hasPermi('system:sign:add')")
    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TourismActiviteSign tourismActiviteSign)
    {
        return toAjax(tourismActiviteSignService.insertTourismActiviteSign(tourismActiviteSign));
    }

    /**
     * 修改活动报名
     */
    @PreAuthorize("@ss.hasPermi('system:sign:edit')")
    @Log(title = "活动报名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismActiviteSign tourismActiviteSign)
    {
        return toAjax(tourismActiviteSignService.updateTourismActiviteSign(tourismActiviteSign));
    }

    /**
     * 删除活动报名
     */
    @PreAuthorize("@ss.hasPermi('system:sign:remove')")
    @Log(title = "活动报名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tourismActiviteSignService.deleteTourismActiviteSignByIds(ids));
    }

    /**
     * 活动报名
     * @param map
     * @param request
     * @return
     */
    @PostMapping("/intertSign")
    public AjaxResult intertSign(@RequestBody Map map, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        Integer userId = loginUser.getUserId();
        TourismUser tourismUser = tourismUserMapper.selectByUserId(userId);
        if(tourismUser == null || userId == null){
            return AjaxResult.error(201,"您还未注册或者登录，请先注册或登录");
        }
        Integer activiteId = (Integer) map.get("activiteId");
        TourismActiviteSign tourismActiviteSign1 = tourismActiviteSignMapper.selectByIdAndUserId(activiteId, userId);
        if(tourismActiviteSign1 != null){
            return AjaxResult.error("您已经报名了该活动！");
        }
        TourismActiviteSign tourismActiviteSign = new TourismActiviteSign();
        tourismActiviteSign.setActiviteId(activiteId);
        tourismActiviteSign.setUserId(userId);
        tourismActiviteSignService.insertTourismActiviteSign(tourismActiviteSign);
        return AjaxResult.success("报名成功！");

    }

    /**
     * 根据用户id查询用户报名活动记录
     * @param request
     * @return
     */
    @PostMapping("/selectSign")
    public AjaxResult selectSign(HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser == null){
            return AjaxResult.error(201,"您还未注册或者登录，请先注册或登录");
        }else {
            TourismUser tourismUser = tourismUserMapper.selectByUserId(loginUser.getUserId());
            if (tourismUser == null || loginUser.getUserId() == null) {
                return AjaxResult.error(201, "您还未注册或者登录，请先注册或登录");
            } else {
                List<TourismActivities> tourismActivities = activitiesMapper.findById(loginUser.getUserId());
                if(tourismActivities.size() == 0 || tourismActivities == null){
                    return AjaxResult.error("您还没有收藏任何活动信息！");
                }
                return AjaxResult.success(tourismActivities);
             }
            }
        }

}
